//
//  ZMYForgetPasswdViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-28.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYForgetPasswdViewController.h"
#import "ZMYLoginConfirmViewController.h"
#import "UIViewControllerAdditions.h"
#import "UINavigationController+SGProgress.h"
#import "Config.h"

@interface ZMYForgetPasswdViewController ()

@end

@implementation ZMYForgetPasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    [self setLeftBackButtonForNavigation];
    if (self.isBindingPhone) {
        [self setTitleForNavigation:@"绑定手机"];
    } else {
        [self setTitleForNavigation:@"忘记密码"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//手机号码验证
-(BOOL)validateMobile:(NSString *)mobile
{
    //手机号以1开头，10个 \d 数字字符
    NSString *phoneRegex = @"^1\\d{10}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
- (IBAction)toNext:(id)sender {
    NSString *mobileStr = self.textField.text;
    if ([self validateMobile:mobileStr]) {
        [self.textField resignFirstResponder];
        [self.navigationController setSGProgressPercentage:60 andTintColor:RGB_COLOR(255, 114, 0)];
        if (self.isBindingPhone) {
            [[ZMYNetWorkAPI default]userBindingPhone:mobileStr response:^(NSDictionary *resultDic) {
                [self.navigationController setSGProgressPercentage:100];
                ZMYLoginConfirmViewController *new = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYLoginConfirmViewController"];
                new.result = resultDic;
                [self.navigationController pushViewController:new animated:YES];
            } error:^(NSString *errorStr) {
                [self.navigationController cancelSGProgress];
                [AppDelegate showHUDAndHide:errorStr view:self.view];
            }];
        } else {
            [[ZMYNetWorkAPI default]forgetPWgetSMS:mobileStr response:^(NSDictionary *resultDic) {
                [self.navigationController setSGProgressPercentage:100];
                ZMYLoginConfirmViewController *new = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYLoginConfirmViewController"];
                new.result = resultDic;
                [self.navigationController pushViewController:new animated:YES];
            } error:^(NSString *errorStr) {
                [self.navigationController cancelSGProgress];
                [AppDelegate showHUDAndHide:errorStr view:self.view];
            }];
        }
    } else {
        [AppDelegate showHUDAndHide:@"请输入手机号" view:self.view];
    }
}

@end
